﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Web.Script.Serialization;
using BUS;
using DAO;
using DTO;

namespace CungMuaC5.Account_Admin
{
    public partial class ThongKeDoanhThu : System.Web.UI.Page
    {
        public List<float> listDoanhThu = new List<float>();
        int year;
        int ToKnow = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                fillDropDownList();
                year = Convert.ToInt32(Get_Year(1));            
            }
        }

        private void fillDropDownList()
        {
            BUS_TaiKhoan bus = new BUS_TaiKhoan();
            List<DTO_DoanhNghiep> list = bus.DoanhNghiep();
            DropDownList3.DataSource = list;
            DropDownList3.DataValueField = "MaDN";
            DropDownList3.DataTextField = "TenDoanhNghiep";
            DropDownList3.DataBind();

        }
        public string Get_Year(int i)
        {
            string str = DateTime.Now.ToString().Trim();
            string str1 = str.Substring(0, 2);
            try
            {
                int a = Convert.ToInt32(str1);
                str = str.Substring(6, 3);
                
            }
            catch (Exception ee)
            {
                str = str.Substring(5, 3);
                
            }
            str = "2" + str;
            if (i == 1)
                return str;
            if (i == 2)
            {
                int a = Convert.ToInt32(str);
                a = a - 1;
                return a.ToString();
            }
            else
                return (new JavaScriptSerializer()).Serialize(str);
        }
        public string getJson(string nam)
        {
            List<float> a = new List<float>();
            BUS_TaiKhoan bus = new BUS_TaiKhoan(1);
            a = bus.DoanhThuTungThang(nam);

            var publicationTable = new List<object>{
             new object[]{ "Jan",a[0]/1000000},
             new object[]{ "Feb",a[1] /1000000},
             new object[]{ "Mar",a[2]/1000000},
             new object[]{ "Apr",a[3] /1000000},
             new object[]{ "May",a[4]/1000000},
             new object[]{ "June",a[5]/1000000 },
             new object[]{ "July",a[6]/1000000 },
             new object[]{ "Aug",a[7] /1000000},
             new object[]{ "Sep",a[8] /1000000},
             new object[]{ "Oct",a[9] /1000000},
             new object[]{ "Nov",a[10]/1000000},
             new object[]{ "Dec",a[11] /1000000},

               };
            
            return (new JavaScriptSerializer()).Serialize(publicationTable);
        }

        public void fill()
        {
            BUS_TaiKhoan bus = new BUS_TaiKhoan(1);

            string year = DropDownList2.SelectedItem.Value;
            string thang = DropDownList1.SelectedItem.Value;
            listDoanhThu = bus.DoanhThuTungThang(year);
            List<DTO_DonHang1> list = bus.DoanhThu(year, thang);
            GridView1.DataSource = list;
            int index = Convert.ToInt16(thang) - 1;
            Label17.Text = listDoanhThu[index].ToString();
            GridView1.DataBind();
        }
        protected void Button1_Click1(object sender, EventArgs e)
        {
            fill(); 
           
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            fill();
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            fill1(1);
            ToKnow = 1;
        }
        protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView2.PageIndex = e.NewPageIndex;
            if(ToKnow==1) fill1(1);
            else fill1(0);
        }
        private void fill1(int i)
        {
            BUS_TaiKhoan bus = new BUS_TaiKhoan(1);
            float tien = 0 ;
            string year = DropDownList5.SelectedItem.Value;
            string thang = DropDownList4.SelectedItem.Value;
            int MaDN = Convert.ToInt32( DropDownList3.SelectedItem.Value);

                List<DTO_DonHang1> list = bus.DoanhThuDoanhNghiep(year, thang,MaDN,i);
                GridView2.DataSource = list;
                for (int j = 0; j < list.Count; j++)
                    tien += list[j].Tien;
                    Label18.Text = tien.ToString();
                    GridView2.DataBind();
        }



       
    }
}